<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
#doc
#   classname:	PHPSSO
#	scope:		PUBLIC
#	author :Yokit QQ:182860914
#/doc

class Phpsso extends SB_Controller
{
	function __construct ()
	{
		parent::__construct();
		$this->load->model('user_m');
		$this->load->library('form_validation');
		
		define('APPID', $this->config->item('phpsso_appid'));
		$this->ps_api_url = $this->config->item('phpsso_api_url');	//接口地址
		$this->ps_auth_key = $this->config->item('phpsso_auth_key');	//加密密钥
		$this->ps_version = $this->config->item('phpsso_version');
		
		$this->load->library('phpssoclient',
			array (
				'ps_api_url' => $this->ps_api_url,
				'ps_auth_key' => $this->ps_auth_key,
				'ps_version' => $this->ps_version
			)
		);
		
		//pc_base::load_app_class('client', 'member', 0);
		//$client = new client($ps_api_url, $ps_auth_key);

	}

	public function index()
	{
		
		$code = $_REQUEST['code'];
		$arr = array();
		parse_str($this->phpssoclient->sys_auth($code, 'DECODE'), $arr);
		
		if(isset($arr['action'])) {
			$action = $arr['action'];
		} else {
			exit('0');
		}
	
		/**
		 * 测试通信状态
		 */
		if ($action == 'check_status') exit('1');
		
		//同步登陆
		if ($action == 'synlogin') $this->synlogin($arr);
		
		//删除用户
		if ($action == 'member_delete') $this->member_delete($arr);
		
		//编辑用户
		if ($action == 'member_edit') $this->member_edit($arr);
		
		//同步退出
		if ($action == 'synlogout') $this->synlogout();
		
	}
	
	/**
	 * 同步登陆
	 */
	private function synlogin($arr)
	{
		if(!isset($arr['uid'])) exit('0');
		
		$phpssouid = $arr['uid'];
		$userinfo = $this->user_m->get_user_by_phpssouid($phpssouid);
		
		if($userinfo['ssoname']!=$arr['username']) exit('0');
		
		if (!$userinfo) {
			//插入会员
			exit;
			$ps_userinfo = $this->phpssoclient->ps_get_member_info($phpssouid);
			$ps_userinfo = unserialize($ps_userinfo);

			if ($ps_userinfo['uid'] > 0) {
				/*require_once MOD_ROOT.'api/member_api.class.php';
				$member_api = new member_api();
				$arr_member['touserid'] = $ps_userinfo['uid'];
				$arr_member['registertime'] = TIME;
				$arr_member['lastlogintime'] = TIME;
				$arr_member['username'] = $ps_userinfo['username'];
				$arr_member['password'] = md5(PASSWORD_KEY.$password) ;
				$arr_member['email'] = $ps_userinfo['email'];
				$arr_member['modelid'] = 10;
				$member_api->add($arr_member);
				$userid = $member->get_userid($arr['username']);
				$userinfo = $member->get($userid);*/
			}

			$username = $ps_userinfo['username'];
		} else {
			$username = $userinfo['username'];
		}
		
		//执行本系统登陆操作
		$userid = $userinfo['uid'];
		$phpssouid = $userinfo['phpssouid'];
		$groupid = $userinfo['gid'];
		$grouptype = $userinfo['group_type'];
		$ssoname = $userinfo['ssoname'];
		//$password = $userinfo['password'];
		//$nickname = $userinfo['nickname'];
		
		$data = array(
			'ssoname' => $ssoname,
			'password' => $password
		);
		header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
		if ($this->user_m->phpssologin($data)) {
			$uid=$this->session->userdata('uid');
			
			//更新积分
			if(time()-@$data['myinfo']['lastlogin']>86400){
				$this->config->load('userset');
				$this->user_m->update_credit($uid,$this->config->item('credit_login'));
			}
			//更新最后登录时间
			$this->user_m->update_user($uid,array('lastlogin'=>time()));
			exit('1');
		} else {
			exit('0');
		}
	}
	
	/**
	 * 删除用户
	 */
	private function member_delete($arr)
	{
		if(!isset($arr['uids'])) exit('0');
		$uidarr = $arr['uids'];
		foreach($uidarr as $uid){
			$this->_member_delete($uid);
		}
	}
	
	/**
	 * 删除用户 单条
	 */
	private function _member_delete($uid)
	{	
		$user=$this->user_m->get_user_by_phpssouid($uid);
		if(!$user){
			exit('0');
		} else{
			$this->db->set('value','value-1',false)->where('item','total_users')->update('site_stats');
			$this->db->set('value','value-'.@$user['topics'],false)->where('item','total_topics')->update('site_stats');
			$this->db->set('value','value-'.@$user['replies'],false)->where('item','total_comments')->update('site_stats');
			$this->user_m->del($user['uid']);
			//更新栏目中的数据
			$this->load->model('cate_m');
			$nodes=$this->cate_m->get_node_ids();
			foreach($nodes as $k=>$v)
			{
				$data[$k]['node_id']=@$v['node_id'];
				$data[$k]['listnum']=$this->db->where('node_id',@$v['node_id'])->count_all_results('topics');
			}
			$this->db->update_batch('nodes', $data, 'node_id');
			if($user['avatar']!='uploads/avatar/default/'){
				@unlink(FCPATH.$user['avatar'].'big.png');
				@unlink(FCPATH.$user['avatar'].'normal.png');
				@unlink(FCPATH.$user['avatar'].'small.png');
			}
			exit('1');
		}
	}
	
	/**
	 * 编辑用户
	 */
	private function member_edit($arr)
	{
		if(!isset($arr['uid'])) exit('0');
		$phpssouid = $arr['uid'];
		$user=$this->user_m->get_user_by_phpssouid($phpssouid);
		if(!$user){
			exit('0');
		}else{
			$userinfo = array();
			if(isset($arr['password'])) {
				$userinfo['password'] = $arr['password'];
				$userinfo['salt'] = $arr['random'];
			}
			if(isset($arr['email']) && !empty($arr['email'])) {
				$userinfo['email'] = $arr['email'];
			}
			
			if(empty($userinfo)) exit('0');
			
			//$userinfo = array ('phpssouid' => $user['uid'], 'password' => $userinfo['password']);
			//$status = $this->user_m->update_pwd($userinfo);
			
			$this->db->where('uid', $user['uid']);
			$this->db->update('users', $userinfo);
			$status = $this->db->affected_rows();

			if($status) {
				exit('1');
			} else {
				exit('0');
			}
		}
	}
	
	/**
	 * 同步退出
	 */
	private function synlogout()
	{
		exit('0');
		/*$this->session->sess_destroy();
		
		$this->load->helper('cookie');
		delete_cookie('uid');
		delete_cookie('username');
		delete_cookie('group_type');
		delete_cookie('gid');
		delete_cookie('openid');
		exit('1');*/
	}

}